import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class ChatMenu extends JMenuBar{
	
	private User user;
	
	private JFrame mainFrame;
	
	private JMenu userMenu;
	private JMenu connectionMenu;
	private JMenu infoMenu;
	private JMenu helpMenu;
	
	private String value ="";
	
	public ChatMenu(JFrame mainFrame ) {
		super();
		
		this.mainFrame = mainFrame;
		
		user = User.getUser();
		
		userMenu = new JMenu("user");
		initUserMenu();
		add(userMenu);
		
		connectionMenu = new JMenu("connection");
		initConnectionMenu();
		add(connectionMenu);
		
		helpMenu = new JMenu("help");
		add(helpMenu);
		
		infoMenu = new JMenu("info");
		add(infoMenu);
		
		
		
	}

	
	public void initUserMenu(){
		
		JMenuItem userNameItem = new JMenuItem("Set name");
		userNameItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				mainFrame.setEnabled(false);
				
				new DataSetter("Name :");

			}
		});
		userMenu.add(userNameItem);
		
		JMenuItem userImmItem = new JMenuItem("Set avatar");
		userMenu.add(userImmItem);
		
	}
	
	
	private void initConnectionMenu(){
		JMenuItem createConnection = new JMenuItem("create new connection");
		createConnection.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainFrame.setEnabled(false);
				new DataSetter("Port :");
			}
		});
		connectionMenu.add(createConnection);
		
		JMenuItem partecipate = new JMenuItem("partecipate existing");
		connectionMenu.add(partecipate);
		
	}
	
	
	private class DataSetter{		
		public DataSetter( String title ) {
			
			
			final JFrame frame = new JFrame();
			JPanel panel = new JPanel();
			frame.getContentPane().add(panel);
			
			
			JLabel label = new JLabel(title);
			panel.add(label);
			
			final JTextField dataInput = new JTextField( 10 );
			panel.add(dataInput);
			
			JButton set = new JButton("OK");
			set.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String[] val = dataInput.getText().split(" ");
					
					try {
						if( val[0].compareTo("") != 0  ){
							value = dataInput.getText();
							switch(title){
							
							case "Port :" : ChatServer server = new ChatServer();
											server.newConnection(Integer.parseInt(value));
											break;
							case "Name :" : user.setUserName( value );
											break;
							}
							
							mainFrame.setEnabled(true);
							frame.dispose();
						}
					} catch (Exception e1) {
						
					}
					
				}
			});
			panel.add(set);
			
			
			JButton cancel = new JButton("Cancel");
			cancel.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					mainFrame.setEnabled(true);
					frame.dispose();
				}
			});
			panel.add(cancel);
			
			frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			frame.pack();
			frame.setLocationRelativeTo(null);
			frame.setVisible(true);
		}
	}
}
